home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / cvs-1.8 / cvs-1 / cvs-1.8.1 / src / expand_path.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-06  |  5.5 KB  |  242 lines

  1. /* expand_path.c -- expand environmental variables in passed in string
  2.  *
  3.  * The main routine is expand_path(), it is the routine that handles
  4.  * the '~' character in four forms: 
  5.  *     ~name
  6.  *     ~name/
  7.  *     ~/
  8.  *     ~
  9.  * and handles environment variables contained within the pathname
  10.  * which are defined by:
  11.  *     ${var_name}   (var_name is the name of the environ variable)
  12.  *     $var_name     (var_name ends w/ non-alphanumeric char other than '_')
  13.  */
  14.  
  15. #include "cvs.h"
  16. #include <sys/types.h>
  17.  
  18. static char *expand_variable PROTO((char *env, char *file, int line));
  19.  
  20.  
  21. /* User variables.  */
  22.  
  23. List *variable_list = NULL;
  24.  
  25. static void variable_delproc PROTO ((Node *));
  26.  
  27. static void
  28. variable_delproc (node)
  29.     Node *node;
  30. {
  31.     free (node->data);
  32. }
  33.  
  34. /* Currently used by -s option; we might want a way to set user
  35.    variables in a file in the $CVSROOT/CVSROOT directory too.  */
  36.  
  37. void
  38. variable_set (nameval)
  39.     char *nameval;
  40. {
  41.     char *p;
  42.     char *name;
  43.     Node *node;
  44.  
  45.     p = nameval;
  46.     while (isalnum (*p) || *p == '_')
  47.     ++p;
  48.     if (*p != '=')
  49.     error (1, 0, "illegal character in user variable name in %s", nameval);
  50.     if (p == nameval)
  51.     error (1, 0, "empty user variable name in %s", nameval);
  52.     name = xmalloc (p - nameval + 1);
  53.     strncpy (name, nameval, p - nameval);
  54.     name[p - nameval] = '\0';
  55.     /* Make p point to the value.  */
  56.     ++p;
  57.     if (strchr (p, '\012') != NULL)
  58.     error (1, 0, "linefeed in user variable value in %s", nameval);
  59.  
  60.     if (variable_list == NULL)
  61.     variable_list = getlist ();
  62.  
  63.     node = findnode (variable_list, name);
  64.     if (node == NULL)
  65.     {
  66.     node = getnode ();
  67.     node->type = VARIABLE;
  68.     node->delproc = variable_delproc;
  69.     node->key = name;
  70.     node->data = xstrdup (p);
  71.     (void) addnode (variable_list, node);
  72.     }
  73.     else
  74.     {
  75.     /* Replace the old value.  For example, this means that -s
  76.        options on the command line override ones from .cvsrc.  */
  77.     free (node->data);
  78.     node->data = xstrdup (p);
  79.     free (name);
  80.     }
  81. }
  82.  
  83. /* This routine will expand the pathname to account for ~ and $
  84.     characters as described above.  If an error occurs, an error
  85.     message is printed via error() and NULL is returned.  FILE and
  86.     LINE are the filename and linenumber to include in the error
  87.     message.  */
  88. char *
  89. expand_path (name, file, line)
  90.     char *name;
  91.     char *file;
  92.     int line;
  93. {
  94.     char *s;
  95.     char *d;
  96.     /* FIXME: arbitrary limit.  */
  97.     char  mybuf[PATH_MAX];
  98.     char  buf[PATH_MAX];
  99.     char *result;
  100.     s = name;
  101.     d = mybuf;
  102.     while ((*d++ = *s))
  103.     if (*s++ == '$')
  104.     {
  105.         char *p = d;
  106.         char *e;
  107.         int flag = (*s == '{');
  108.  
  109.         for (; (*d++ = *s); s++)
  110.         if (flag
  111.             ? *s =='}'
  112.             : isalnum (*s) == 0 && *s != '_')
  113.             break;
  114.         *--d = 0;
  115.         e = expand_variable (&p[flag], file, line);
  116.  
  117.         if (e)
  118.         {
  119.         for (d = &p[-1]; (*d++ = *e++);)
  120.             ;
  121.         --d;
  122.         if (flag && *s)
  123.             s++;
  124.         }
  125.         else
  126.         /* expand_variable has already printed an error message.  */
  127.         return NULL;
  128.     }
  129.     *d = 0;
  130.     s = mybuf;
  131.     d = buf;
  132.     /* If you don't want ~username ~/ to be expanded simply remove
  133.      * This entire if statement including the else portion
  134.      */
  135.     if (*s++ == '~')
  136.     {
  137.     char *t;
  138.     char *p=s;
  139.     if (*s=='/' || *s==0)
  140.         t = get_homedir ();
  141.     else
  142.     {
  143.         struct passwd *ps;
  144.         for (; *p!='/' && *p; p++)
  145.         ;
  146.         *p = 0;
  147.         ps = getpwnam (s);
  148.         if (ps == 0)
  149.         {
  150.         if (line != 0)
  151.             error (0, 0, "%s:%d: no such user %s",
  152.                file, line, s);
  153.         else
  154.             error (0, 0, "%s: no such user %s", file, s);
  155.         return NULL;
  156.         }
  157.         t = ps->pw_dir;
  158.     }
  159.     while ((*d++ = *t++))
  160.         ;
  161.     --d;
  162.     if (*p == 0)
  163.         *p = '/';           /* always add / */
  164.     s=p;
  165.     }
  166.     else
  167.     --s;
  168.     /* Kill up to here */
  169.     while ((*d++ = *s++))
  170.     ;
  171.     *d=0;
  172.     result = xmalloc (sizeof(char) * strlen(buf)+1);
  173.     strcpy (result, buf);
  174.     return result;
  175. }
  176.  
  177. static char *
  178. expand_variable (name, file, line)
  179.     char *name;
  180.     char *file;
  181.     int line;
  182. {
  183.     if (strcmp (name, CVSROOT_ENV) == 0)
  184.     return CVSroot;
  185.     else if (strcmp (name, RCSBIN_ENV)  == 0)
  186.     return Rcsbin;
  187.     else if (strcmp (name, EDITOR1_ENV) == 0)
  188.     return Editor;
  189.     else if (strcmp (name, EDITOR2_ENV) == 0)
  190.     return Editor;
  191.     else if (strcmp (name, EDITOR3_ENV) == 0)
  192.     return Editor;
  193.     else if (strcmp (name, "USER") == 0)
  194.     return getcaller ();
  195.     else if (isalpha (name[0]))
  196.     {
  197.     /* These names are reserved for future versions of CVS,
  198.        so that is why it is an error.  */
  199.     if (line != 0)
  200.         error (0, 0, "%s:%d: no such internal variable $%s",
  201.            file, line, name);
  202.     else
  203.         error (0, 0, "%s: no such internal variable $%s",
  204.            file, name);
  205.     return NULL;
  206.     }
  207.     else if (name[0] == '=')
  208.     {
  209.     Node *node;
  210.     /* Crazy syntax for a user variable.  But we want
  211.        *something* that lets the user name a user variable
  212.        anything he wants, without interference from
  213.        (existing or future) internal variables.  */
  214.     node = findnode (variable_list, name + 1);
  215.     if (node == NULL)
  216.     {
  217.         if (line != 0)
  218.         error (0, 0, "%s:%d: no such user variable ${%s}",
  219.                file, line, name);
  220.         else
  221.         error (0, 0, "%s: no such user variable ${%s}",
  222.                file, name);
  223.         return NULL;
  224.     }
  225.     return node->data;
  226.     }
  227.     else
  228.     {
  229.     /* It is an unrecognized character.  We return an error to
  230.        reserve these for future versions of CVS; it is plausible
  231.        that various crazy syntaxes might be invented for inserting
  232.        information about revisions, branches, etc.  */
  233.     if (line != 0)
  234.         error (0, 0, "%s:%d: unrecognized varaible syntax %s",
  235.            file, line, name);
  236.     else
  237.         error (0, 0, "%s: unrecognized varaible syntax %s",
  238.            file, name);
  239.     return NULL;
  240.     }
  241. }
  242.